﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER
{
    public partial class FormPassword : Form
    {
        public FormPassword()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        public void ShowForm()
        {
            tbxPW.Text = "";
            lblNotice.Text = "";
            this.ShowDialog();
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                //if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                //{
                //    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                //}

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "Confirm":
                    {
                        PassWordConfirm();
                    }
                    break;
                case "Cancel":
                    {
                        tbxPW.Text = "";
                        lblNotice.Text = "";
                        this.Visible = false;
                    }
                    break;
            }
        }

        private void tbxPW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PassWordConfirm();
            }
        }

        private void PassWordConfirm()
        {
            string str = tbxPW.Text.Trim();
            if (DateTime.Now.ToString("MMdd") == str)
            {
                Kernel.PW = 1;
                Kernel.OP.Level = 1;
                tbxPW.Text = "";
                lblNotice.Text = "";
                this.Visible = false;
            }
            else
            {
                lblNotice.Text = "密码错误";
                Kernel.PW = 0;
            }
        }
    }
}
